package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.acceptRFC;

import java.sql.ResultSet;
import java.util.ArrayList;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.WorkOrderMaker;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.database.DatabaseConnection;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.User;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.Workorder;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.acceptRFCPanel.AcceptRFCPanel;

public class ActualiseListsThread extends Thread {

	private AcceptRFCPanel acceptRFCPanel;

	private DatabaseConnection connection;
	private User user;

	public ActualiseListsThread(AcceptRFCPanel acceptRFCPanel, User signedInUser) {
		this.acceptRFCPanel = acceptRFCPanel;
		this.user = signedInUser;
	}

	@Override
	public void run() 
	{
		listAcceptedRFC();
	}

	private void listAcceptedRFC()
	{
		this.connection = new DatabaseConnection();
		this.connection.connect();
		String query1 = "SELECT * FROM `"+Workorder.WORKORDER_TABLE+"` WHERE `" + Workorder.TC_RESPONIBLE
				+ "` = '"+ user.getFullName()+"' AND `"+Workorder.TC_WO_STATUS+"` = 'assigned'";
		
		ResultSet rs1 = connection.query(query1);
		
		this.acceptRFCPanel.getListPanel().getApprovedDisplayTable().clearTable();
		
		int counter1 = 0;
		
		ArrayList<Workorder> wo = new ArrayList<Workorder>();
		
		try
		{
			while (rs1.next())
			{
				counter1++;
				WorkOrderMaker maker = new WorkOrderMaker();
				wo.add(maker.makeanewWorkrder(rs1));
			}
			if (counter1 == 0)
			{
				
				this.acceptRFCPanel.getListPanel().getApprovedDisplayTable().displayNoElenemt();
			}
		}
		catch(Exception e)
		{
			System.out.println("wrong code 1");
		}
		this.acceptRFCPanel.getListPanel().getApprovedDisplayTable().setVisible(false);
		for (Workorder workorder : wo)
		{
			this.acceptRFCPanel.getListPanel().getApprovedDisplayTable().addWorkOrderInTable(workorder);
		}
		this.acceptRFCPanel.getListPanel().getApprovedDisplayTable().setVisible(true);
		
		String name = user.getFirstname() + " " + user.getName();
		
		String query2 = "SELECT * FROM `"+Workorder.WORKORDER_TABLE+"` WHERE `" +Workorder.TC_WO_STATUS
				+ "` = 'in progress' AND " + Workorder.TC_RESPONIBLE + " = '"
				+ name + "'";
		
		ResultSet rs2 = connection.query(query2);
		
		this.acceptRFCPanel.getListPanel().getWorkingDisplayTable().clearTable();
		
		ArrayList<Workorder> wo2 = new ArrayList<Workorder>();
		int counter2 = 0;
		try
		{
			while(rs2.next())
			{
				counter2++;
				WorkOrderMaker maker = new WorkOrderMaker();
				wo2.add(maker.makeanewWorkrder(rs2));
			}
			if(counter2 == 0)
			{
				this.acceptRFCPanel.getListPanel().getWorkingDisplayTable().displayNoElenemt();
			}
		}
		catch(Exception e)
		{
			System.out.println("wrong code 2");
		}
		this.acceptRFCPanel.getListPanel().getWorkingDisplayTable().setVisible(false);
		for (Workorder workorder : wo2)
		{
			this.acceptRFCPanel.getListPanel().getWorkingDisplayTable().addWorkOrderInTable(workorder);
		}
		this.acceptRFCPanel.getListPanel().getWorkingDisplayTable().setVisible(true);
		this.connection.disconnect();
		this.acceptRFCPanel.getFunctionpanel().getProgress().setVisible(false);
	}
}